package com.eportal.struts.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.eportal.ORM.MpaySalesInfo;
import com.eportal.ORM.MpayUser;
import com.eportal.ORM.UserView;
import com.eportal.core.BaseAction;
import com.eportal.core.Page;
import com.eportal.core.Util;
import com.eportal.service.AdminService;

public class SalesAction extends BaseAction {
	AdminService service;
	AdminService serviceOracle;
	private Page page;
	private String start = "0";
	private String limit = "30";
	private boolean success;
	private String message;
	
	private int id;
	private String trace;
	private String name;
	private String tel;
	private String address;
	private String amt;
	private String remark;
	private String createUser;
	private String card;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCard() {
		return card;
	}
	public void setCard(String card) {
		this.card = card;
	}
	public String getTrace() {
		return trace;
	}
	public void setTrace(String trace) {
		this.trace = trace;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getAmt() {
		return amt;
	}
	public void setAmt(String amt) {
		this.amt = amt;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getCreateUser() {
		return createUser;
	}
	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}
	public AdminService getService() {
		return service;
	}
	public void setService(AdminService service) {
		this.service = service;
	}
	public AdminService getServiceOracle() {
		return serviceOracle;
	}
	public void setServiceOracle(AdminService serviceOracle) {
		this.serviceOracle = serviceOracle;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getCashTrace(){
		LOG.info("查询现金单号。。");
		List tmp = service.query("from MpaySalesInfo where trace like 'C%' order by trace desc");
		if(tmp.size()==0){
			message="C"+Util.getCurrentDate("yyyyMMdd")+"000001";
		}else{
			MpaySalesInfo m = (MpaySalesInfo) tmp.get(0);
			message="C"+Util.getCurrentDate("yyyyMMdd")+Util.addZero((Integer.valueOf(m.getTrace().substring(9))+1)+"",6);
		}
		if(message.endsWith("999999")){
			message="C"+Util.getCurrentDate("yyyyMMdd")+"000001";
		}
		return SUCCESS;
	}
	public String salesadd(){
		LOG.info("保存销售单。。");
		UserView m = (UserView) getRequest().getSession().getAttribute(
		"mpay_user");
		MpaySalesInfo sale = new MpaySalesInfo();
		sale.setAddress(address==null?"":address);
		sale.setAmt(amt);
		sale.setCard(card==null?"-":card);
		sale.setCreateUser(createUser);
		sale.setName(name==null?"":name);
		sale.setRemark(remark);
		sale.setTel(tel==null?"":tel);
		sale.setTrace(trace);
		sale.setUserid(m.getId()+"");
		try {
			service.save(sale);
			success=true;
			message="保存销售单成功！";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success=false;
			message="保存销售单错误，可能该交易已经添加！";
		}
		return SUCCESS;
	}
	public String salesinfo(){
		LOG.info("读取销售单信息。。。");
		UserView m = (UserView) getRequest().getSession().getAttribute(
		"mpay_user");
		int s = Integer.valueOf(start);
		int l = Integer.valueOf(limit);
		String sql = "";
		if (m.getIsAll() == 1) {
			sql += " where '1'='1' ";
		} else {
			sql += " where userid = '" + m.getId() + "' ";
		}
		if(trace!=null&&!trace.equals("")){
			sql+=" and trace like '%"+trace+"%'";
		}
		int start = 1;
		if (s != 0)
			start = s / 30 + 1;
		sql += " order by id desc ";
		System.out.println(sql);
		List userList = service.query("from MpaySalesInfo" + sql,
				start, l);
		page = new Page();
		page.setRoot(userList);
		page.setTotalProperty(service
				.countQuery("select count(*) from MpaySalesInfo" + sql));
		page.setStart(++s);
		page.setLimit(l = l == 0 ? 30 : l);
		return SUCCESS;
	}
	public InputStream salesToexcel(){
		LOG.info("导出销售单。。。");
		UserView m = (UserView) getRequest().getSession().getAttribute(
		"mpay_user");
		String sql = "";
		if (m.getIsAll() == 1) {
			sql += " where '1'='1' ";
		} else {
			sql += " where userid = '" + m.getId() + "' ";
		}
		if(trace!=null&&!trace.equals("")){
			sql+=" and trace like '%"+trace+"%'";
		}
		sql += " order by id desc ";
		List sales = service.query("from MpaySalesInfo "+sql);
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("new sheet");
		HSSFRow row = sheet.createRow((short) 0);
		HSSFCell cell = row.createCell((short) 0);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("单号");
		cell = row.createCell((short) 1);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("金额");
		cell = row.createCell((short) 2);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("类型");
		cell = row.createCell((short) 3);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("刷卡卡号");
		cell = row.createCell((short) 4);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("内容");
		cell = row.createCell((short) 5);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("客户姓名");
		cell = row.createCell((short) 6);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("联系电话");
		cell = row.createCell((short) 7);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("地址");
		cell = row.createCell((short) 8);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("填单人");
		short rowNum = 1;
		for(int i = 0;i<sales.size();i++){
			MpaySalesInfo s = (MpaySalesInfo)sales.get(i);
			row = sheet.createRow(rowNum);
			cell = row.createCell((short) 0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getTrace());
			cell = row.createCell((short) 1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getAmt());
			cell = row.createCell((short) 2);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getTrace().startsWith("T")?"刷卡":"现金");
			cell = row.createCell((short) 3);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getCard()==null?"-":s.getCard());
			cell = row.createCell((short) 4);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getRemark());
			cell = row.createCell((short) 5);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getName()==null?"-":s.getName());
			cell = row.createCell((short) 6);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getTel()==null?"-":s.getTel());
			cell = row.createCell((short) 7);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getAddress()==null?"-":s.getAddress());
			cell = row.createCell((short) 8);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(s.getCreateUser());
			rowNum++;
		}
		try {
			OutputStream out = this.getResponse().getOutputStream();
			this.getResponse().setHeader("Content-disposition",
					"attachment; filename=sales.xls");
			this.getResponse().setContentType("application/msexcel;charset=UTF-8");
			wb.write(out);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String deleteSales(){
		try {
			service.delById(MpaySalesInfo.class, id);
			message="删除成功!";
			success=true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message="删除失败";
			success=false;
		}
		return SUCCESS;
	}
}
